/*
 *  FILE: 
 *  	file.c
 *
 *  DESCRIPTION:
 *  	Simple file/directory probing functions.  Along with the one string function: fileext().
 *      Also, some macros are defined.
 *
 *  FUNCTIONS:
 *      IS_READABLE()     -  Test to see if the file/directory is readable by the current user.
 *  	fileext()         -  A function capable of stripping the extension off of a filename.  It also
 *  	                     supports ".tar.gz" style extensions.
 *  	getfilesize()     -  Probe the size of any given file, using the filename.
 *  	                     32bit systems should define -D_LARGEFILE_SOURCE for support
 *  	                     for files larger than 4294967295 bytes (4 GB).
 *  	file_attributes() -  Simple function used to grab the attributes of a file stat.
 *  	is_file()         -  Test if filename is a file.
 *  	is_dir()          -  Test if filename is a directory.
 *
 *
 * Copyright (c) 2004-2006 Karlan Thomas Mitchell<karlanmitchell@gmail.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *         This product includes software developed by
           Karlan Thomas Mitchell<karlanmitchell@gmail.com>
 * 4. Neither Karlan Thomas Mitchell nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY KARLAN THOMAS MITCHELL AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "file.h"
		

#ifndef __WIN32__
int IS_READABLE(char * file_name){
	uid_t  this_user   = geteuid();
	gid_t  this_group  = getegid();
	struct stat st_stat;
	stat(file_name, &st_stat);

	if(this_user == st_stat.st_uid)
		return (st_stat.st_mode & S_IRUSR);
	else if(this_user == st_stat.st_gid)
		return (st_stat.st_mode & S_IRGRP);
	else 
		return (st_stat.st_mode & S_IROTH);
}
#endif

void fileext(char * file, char * ext, size_t extpntlen){
	size_t end = strlen(file);
	if(end <= 0)
		return;

	size_t count = end;
	size_t near_last_dot = 0;
	size_t last_dot = 0;
	char * cpypnt;
	
	/*Don't include very first character*/
	for(; count > 0 ;count--){
		if(file[count] == '.'){                       /*Working backwards we found a dot*/
			if(last_dot == 0)                     /*Check if this is the last dot*/
				last_dot = count;             
			else if(near_last_dot == 0)           /*Check if this is the second to last dot*/
				near_last_dot = count;
			else                                  /*everything set, we are done here*/
				break;
		}
		
	}
	

	
	if(near_last_dot != 0)
		cpypnt = &file[near_last_dot + 1];
	else if(last_dot != 0)
		cpypnt = &file[last_dot + 1];
	else
		cpypnt = &file[end];
	strlcpy(ext, cpypnt, extpntlen);
	
}

off_t getfilesize(char * filename)
{
     
     struct stat testfile;
     if(!stat(filename,&testfile))
     {
         return testfile.st_size;
     }
     return (off_t)0;
}
#ifndef WIN32
mode_t file_attributes(char * tFile){
		struct stat st_stat;
		stat(tFile, &st_stat);
		return st_stat.st_mode;
		}
#endif
int display_file(char * file){
	if(is_dir(file))
		return 0;

	FILE * testfile = fopen(file, "r");
	if(testfile == NULL)
		return 0;
	char * tbuffer = (char *)malloc(1024 * sizeof(char));

	size_t rv = 1;
	
	while(rv > 0){
		rv = fread (tbuffer, 1, 1024, testfile);
		     fwrite(tbuffer, 1,   rv, stdout);
	}

	free(tbuffer);
	fclose(testfile);
	return 1;

	}

#ifdef __WIN32__
	#define IS_FILE_OPEN
	#define IS_DIR_OPEN
#elif __MACOSX__
	/*Darwin Kernel Version 8.0.0 Power Macintosh (only confirmed) reports by S_ISREG always true for files AND NON EXISTENT FILES*/
	#define IS_FILE_OPEN
#endif

// #ifdef IS_FILE_OPEN
int is_file(char * file){
	if(is_dir(file))
		return 0;
	FILE * testfile = fopen(file, "r");
	if(testfile == NULL)
		return 0;
	fclose(testfile);
	return 1;

	}
// #else
		
// int is_file(char * filename){
// 		return ( S_ISREG( file_attributes(filename) ) );
// 		}
// #endif

//#ifdef IS_DIR_OPEN
int is_dir(char * path){
	DIR * testdir = opendir(path);
	if(testdir == NULL)
		return 0;
	closedir(testdir);
	return 1;
	}
// #else
// int is_dir(char * dirname){
// 		return ( S_ISDIR( file_attributes(dirname) ) );
// 		}
// #endif


/*
int is_exe(char * file){
	struct stat stat_test;
	stat(file, &stat_test);
	return (stat_test.st_mode & S_IXUSR);
	}*/
